package com.shuai.jit.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shuai.jit.entity.course.Comment;
import com.shuai.jit.entity.vo.VoComment;
import com.shuai.jit.service.CommentService;
import com.shuai.jit.mapper.CommentMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Transactional
@Slf4j
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {


    /**
     * 添加评论
     *
     * @param courseComment 评论内容
     * @return
     */
    @Override
    public Integer addComment(Comment courseComment) {
        log.info(courseComment.toString());
        // 如果pid 为空之间返回
        if(StringUtils.isEmpty(courseComment.getCommentPid())){
           return -1;
        }
        if(StringUtils.isEmpty(courseComment.getCommentContent())){
            return -1;
        }
        courseComment.setCommentGood(0);
        courseComment.setCommentBad(0);
        return this.baseMapper.insert(courseComment);
    }

    /**
     * 获取文章的所有评论
     *
     * @param id 文章或者课程id
     * @return
     */
    @Override
    public List<VoComment> selectComment(String id) {
        List<VoComment> voCommentList = this.baseMapper.selectComment(id);

        if (voCommentList != null) {
            for (int i = 0; i < voCommentList.size(); i++) {
                List<VoComment> temp = this.baseMapper.selectComment(voCommentList.get(i).getCommentId());
                voCommentList.get(i).setVoCommentList(temp);
            }
            return voCommentList;
        }
        return null;
    }

    /**
     * 根据评论，删除文章id
     *
     * @param commentId
     * @return
     */
    @Override
    public Integer deleteByCommentId(String commentId) {

       Comment courseComment = this.baseMapper.selectById(commentId);
        int result = -1;
        if (courseComment != null) {
            QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("pid", courseComment.getCommentId());
            List<Comment> list = this.baseMapper.selectList(queryWrapper);
            if (list != null) {
                for (int i = 0; i < list.size(); i++) {
                    this.baseMapper.deleteById(list.get(i));
                }
            }
            result = this.baseMapper.deleteById(commentId);
        }
        return result;
    }

    /**
     * 根据文章，删除评论
     *
     * @param id
     * @return
     */
    @Override
    public Integer delete(String id) {

         int result = 0;
        List<VoComment> voCommentList = this.baseMapper.selectComment(id);
        if (voCommentList != null) {
            for (VoComment voComment : voCommentList) {
                List<VoComment> temp = this.baseMapper.selectComment(voComment.getCommentId());
                if (temp != null) {
                    for (VoComment comment : temp) {
                        result = this.baseMapper.deleteById(comment.getCommentId());
                    }
                }
                result = this.baseMapper.deleteById(voComment.getCommentId());
            }
            return result;
        }
        return result;
    }
}
